home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / DClap / DTabSelect.cpp < prev    next >
Text File  |  1996-07-05  |  10KB  |  409 lines

  1. // DTabSelect.cp
  2. // d.g.gilbert
  3.  
  4. // this class is used a lot for various display/user interaction arrays of items
  5. // still needs work for multiple selections, run arrays for height/width?
  6. // 
  7.  
  8. #include "DTableView.h"
  9. #include <DPanel.h>
  10. #include <DUtil.h>
  11. #include <DApplication.h>
  12. #include <Dvibrant.h>
  13. #include <DTracker.h>
  14. #include <ncbi.h>
  15. #include <dgg.h>
  16.  
  17.  
  18.  
  19.  
  20.  
  21. DTabSelector::DTabSelector( DTableView* itsTable)
  22. {
  23.     ITabSelector( itsTable); // in constructor !?
  24. }
  25.  
  26. DTabSelector::~DTabSelector() 
  27. {
  28. #if 0
  29.     fOldSelection= Nlm_DestroyRgn( fOldSelection);
  30.     fNewSelection= Nlm_DestroyRgn( fNewSelection);
  31. #endif
  32. }
  33.  
  34. void DTabSelector::ITabSelector( DTableView* itsTable)
  35. {
  36.     ITracker( cTabSelCmd, itsTable, "selection", true, false, itsTable); 
  37.     // ITracker() calls Reset()
  38. }
  39.  
  40.  
  41. void DTabSelector::Reset()
  42. {
  43.     DTracker::Reset();
  44.     if (gKeys->shift()) fDoExtend=  DTabSelection::kExtendSingle;
  45.     else if (gKeys->command()) fDoExtend=  DTabSelection::kExtendMulti;
  46.     else fDoExtend=  DTabSelection::kDontExtend;
  47. #if 1
  48.     fOldSelection= ((DTableView*)fView)->GetSelRect();
  49.     fNewSelection= fOldSelection;
  50. #else
  51.     fOldSelection= Nlm_NewRgn(); 
  52.     fNewSelection= Nlm_NewRgn();  
  53.     CopyRgn( ((DTableView*)fView)->fSelections, fOldSelection);
  54.     CopyRgn( fOldSelection, fNewSelection);
  55. #endif
  56. }
  57.  
  58.  
  59. void DTabSelector::DoItWork()  
  60. {
  61.     if (fMovedOnce || fDoExtend) {
  62.         ((DTableView*)fView)->SelectCells( fNewSelection, 
  63.                 fDoExtend,  DTabSelection::kHighlight,  DTabSelection::kSelect);
  64.         }
  65. }
  66.  
  67. void DTabSelector::UndoWork()  
  68. {
  69. #if 1
  70.     Nlm_RecT tmpRect= fNewSelection;
  71.     fNewSelection= fOldSelection;
  72.     fOldSelection= tmpRect;
  73. #else
  74.     Nlm_RgN tmpRgn= fNewSelection;
  75.     fNewSelection= fOldSelection;
  76.     fOldSelection= tmpRgn;
  77. #endif
  78.     DoItWork();
  79. }
  80.  
  81.  
  82. void DTabSelector::DoIt()  
  83. {
  84.     DCommand::DoIt();
  85.     
  86.     Nlm_PoinT pt1, pt2;
  87.     Nlm_RecT    selr;
  88.     long row, col;
  89.         // +1 is messy fix to stop left/top -1 creap !
  90.     pt1.x= MIN( fAnchorPoint.x+1, fNextPoint.x);
  91.     pt2.x= MAX( fAnchorPoint.x-1, fNextPoint.x);
  92.     pt1.y= MIN( fAnchorPoint.y+1, fNextPoint.y);
  93.     pt2.y= MAX( fAnchorPoint.y-1, fNextPoint.y);
  94.  
  95.     ((DTableView*)fView)->PointToCell( pt1, row, col);
  96.     selr.top=row; selr.left= col;
  97.     ((DTableView*)fView)->PointToCell( pt2, row, col);
  98.     selr.bottom= row; selr.right= col;
  99.     selr.bottom++;
  100.     selr.right++;
  101.     fNewSelection= selr;
  102.     if (!fDoExtend) {
  103.         if (gKeys->shift()) fDoExtend=  DTabSelection::kExtendSingle;
  104.         else if (gKeys->command()) fDoExtend=  DTabSelection::kExtendMulti;
  105.         }
  106.     
  107.     DoItWork();
  108. }
  109.  
  110. void DTabSelector::Undo()  
  111. {
  112.     DCommand::Undo();
  113.     short saveext= fDoExtend;
  114.     fDoExtend= DTabSelection::kDontExtend;
  115.     UndoWork();
  116.     fDoExtend= saveext;
  117. }
  118.  
  119. void DTabSelector::Redo()  
  120. {
  121.     DCommand::Redo();
  122.     UndoWork();
  123. }
  124.  
  125.  
  126.  
  127.  
  128. //----------------------------
  129.  
  130. DTabSelection::DTabSelection(DTableView* itsTable, long nrows, long ncols):
  131.         fTable(itsTable),  fMaxRows(nrows), fMaxCols(ncols), 
  132.         fSelrgn(NULL), fSelectedRow(kNoSelection), fSelectedCol(kNoSelection),
  133.         fCanSelectRow(true), fCanSelectCol(false)
  134. {
  135.     fSelrgn= Nlm_CreateRgn();
  136.     fWorkrgn= Nlm_CreateRgn();
  137.     SetEmptySelection(false); 
  138. }
  139.         
  140. DTabSelection::~DTabSelection()
  141. {
  142.     fSelrgn= Nlm_DestroyRgn( fSelrgn);
  143.     fWorkrgn= Nlm_DestroyRgn( fWorkrgn);
  144. }
  145.  
  146. void DTabSelection::SetTableSize( long rows, long cols)
  147. {
  148.     fMaxRows= rows; 
  149.     fMaxCols= cols;
  150. }
  151.  
  152. void DTabSelection::ChangeRowSize( long atrow, long deltarows)
  153. {
  154.     long newrows= Max(0, fMaxRows + deltarows);
  155.     SetTableSize( newrows, fMaxCols);
  156. }
  157.  
  158. void DTabSelection::ChangeColSize( long atcol, long deltacols)
  159. {
  160.     long newcols= Max(0, fMaxCols + deltacols);
  161.     SetTableSize( fMaxRows, newcols);
  162. }
  163.  
  164.         
  165. void DTabSelection::SetCanSelect(Boolean canrow, Boolean cancol)
  166. {
  167.     fCanSelectRow= canrow;
  168.     fCanSelectCol= cancol;
  169. }
  170.  
  171.  
  172. void DTabSelection::SelectCells( long row, long col, 
  173.                         short extend, Nlm_Boolean highlight, Nlm_Boolean select)
  174. {
  175.     if (row>=fMaxRows || row<0 || col>=fMaxCols || col<0) return;
  176.  
  177.     if (highlight) InvertSelection(); // hide old
  178.     if (fCanSelectRow) fSelectedRow= row; 
  179.     if (fCanSelectCol) fSelectedCol= col;
  180.              
  181.     if (extend == kExtendMulti) {
  182.         if (fSelrect.top == kNoSelection) fSelrect.top= row;
  183.         else if (fSelrect.top > row) fSelrect.top= row;
  184.         if (fSelrect.bottom <= row) fSelrect.bottom= row+1; 
  185.     
  186.         if (fSelrect.left == kNoSelection) fSelrect.left= col;
  187.         else if (fSelrect.left > col) fSelrect.left= col;
  188.         if (fSelrect.right <= col) fSelrect.right= col+1; 
  189.  
  190.         Nlm_LoadRectRgn(fWorkrgn, col, row, col+1, row+1); //?? +1
  191.         if (select) Nlm_UnionRgn( fSelrgn, fWorkrgn, fSelrgn);
  192.         else Nlm_DiffRgn( fSelrgn, fWorkrgn, fSelrgn); //?? Sect or Diff or ??
  193.         }
  194.         
  195.     else if (extend == kExtendSingle) {
  196.         if (fSelrect.top == kNoSelection) fSelrect.top= row;
  197.         else if (fSelrect.top > row) fSelrect.top= row;
  198.         if (fSelrect.bottom <= row) fSelrect.bottom= row+1; 
  199.     
  200.         if (fSelrect.left == kNoSelection) fSelrect.left= col;
  201.         else if (fSelrect.left > col) fSelrect.left= col;
  202.         if (fSelrect.right <= col) fSelrect.right= col+1; 
  203.  
  204.         //Nlm_ClearRgn(fSelrgn);
  205.         //Nlm_LoadRectRgn(fSelrgn, fSelrect.left, fSelrect.top, fSelrect.right, fSelrect.bottom);
  206.         }
  207.         
  208.     else if (select) {
  209.         fSelrect.left= col;
  210.         fSelrect.right= col+1; 
  211.         fSelrect.top= row;
  212.         fSelrect.bottom= row+1; 
  213.         
  214.         Nlm_ClearRgn(fSelrgn);
  215.         //Nlm_LoadRectRgn(fSelrgn, fSelrect.left, fSelrect.top, fSelrect.right, fSelrect.bottom);
  216.         }
  217.     else
  218.         SetEmptySelection(false);
  219.             
  220.     Boolean didscroll= fTable->ScrollIntoView( fSelrect);
  221.     if (!didscroll && highlight) InvertSelection(); // show new
  222. }
  223.  
  224. void DTabSelection::SelectCells( Nlm_RecT selrect,  
  225.                         short extend, Nlm_Boolean highlight, Nlm_Boolean select)
  226. {
  227.     if (selrect.top>= fMaxRows || selrect.bottom<0 
  228.      || selrect.left>=fMaxCols || selrect.right<0) return;
  229.     selrect.left= Max(0,selrect.left);
  230.     selrect.right= Min(fMaxCols, selrect.right);
  231.     selrect.top= Max(0,selrect.top);
  232.     selrect.bottom= Min(fMaxRows, selrect.bottom);
  233.         
  234.     if (highlight) InvertSelection();  
  235.     if (fCanSelectRow) fSelectedRow= selrect.top; 
  236.     if (fCanSelectCol) fSelectedCol= selrect.left;
  237.     
  238.     if (extend == kExtendMulti) {
  239.         if (fSelrect.top == kNoSelection) fSelrect.top= selrect.top;
  240.         if (fSelrect.bottom == kNoSelection) fSelrect.bottom= selrect.bottom;
  241.         if (fSelrect.left == kNoSelection) fSelrect.left= selrect.left;
  242.         if (fSelrect.right == kNoSelection) fSelrect.right= selrect.right;
  243.         if (select) Nlm_UnionRect( &fSelrect, &selrect, &fSelrect);
  244.         else Nlm_SectRect( &fSelrect, &selrect, &fSelrect);
  245.  
  246.         Nlm_LoadRectRgn(fWorkrgn, selrect.left, selrect.top, selrect.right, selrect.bottom);
  247.         if (select) Nlm_UnionRgn( fSelrgn, fWorkrgn, fSelrgn);
  248.         else Nlm_DiffRgn( fSelrgn, fWorkrgn, fSelrgn); // ?? Nlm_SectRgn or Nlm_DiffRgn
  249.         }
  250.         
  251.     else if (extend == kExtendSingle) {
  252.         if (fSelrect.top == kNoSelection) fSelrect.top= selrect.top;
  253.         if (fSelrect.bottom == kNoSelection) fSelrect.bottom= selrect.bottom;
  254.         if (fSelrect.left == kNoSelection) fSelrect.left= selrect.left;
  255.         if (fSelrect.right == kNoSelection) fSelrect.right= selrect.right;
  256.         if (select) Nlm_UnionRect( &fSelrect, &selrect, &fSelrect);
  257.         else Nlm_SectRect( &fSelrect, &selrect, &fSelrect);
  258.  
  259.         //Nlm_LoadRectRgn(fSelrgn, fSelrect.left, fSelrect.top, fSelrect.right, fSelrect.bottom);
  260.       }
  261.     else if (select) {
  262.         fSelrect= selrect; // ?? add +1 to .right & .bottom ??
  263.         Nlm_ClearRgn(fSelrgn);
  264.         //Nlm_LoadRectRgn(fSelrgn, fSelrect.left, fSelrect.top, fSelrect.right, fSelrect.bottom);
  265.         }
  266.     else
  267.         SetEmptySelection(false);
  268.         
  269.     Boolean    didscroll= fTable->ScrollIntoView( fSelrect);
  270.     if (!didscroll && highlight) InvertSelection();  
  271. }
  272.  
  273.  
  274. void DTabSelection::SetEmptySelection(Boolean redraw)
  275. {
  276.     if (redraw) {
  277.         //InvertSelection();
  278.         InvalidateSelection();
  279.         }
  280.     fSelectedRow = kNoSelection;
  281.     fSelectedCol = kNoSelection;
  282.     //Nlm_SetRect
  283.     Nlm_LoadRect( &fSelrect, kNoSelection, kNoSelection, kNoSelection, kNoSelection);
  284.     Nlm_ClearRgn( fSelrgn);
  285. }
  286.  
  287. void DTabSelection::InvalidateSelection()
  288. {
  289.     Nlm_RecT r, r2;
  290.     if (!Nlm_EmptyRgn( fSelrgn)) {
  291.         r2= fSelrect;
  292.         fTable->GetCellRect( r2, r);
  293.         fTable->InvalRect( r);
  294.         }
  295.     else if (!Nlm_EmptyRect(&fSelrect)) {
  296.         r2= fSelrect;
  297.         fTable->GetCellRect( r2, r);
  298.         fTable->InvalRect( r);
  299.         }
  300.     else if (fSelectedRow>=0 && fSelectedCol>=0) {
  301.         fTable->GetCellRect( fSelectedRow, fSelectedCol, r);
  302.         fTable->InvalRect( r);
  303.         }
  304.     else if (fSelectedRow>=0) {
  305.         fTable->GetRowRect( fSelectedRow, r);
  306.         fTable->InvalRect( r);
  307.         }
  308.     else if (fSelectedCol>=0) {
  309.         fTable->GetRowRect( fSelectedCol, r);
  310.         fTable->InvalRect( r);
  311.         }
  312. }
  313.  
  314. Boolean DTabSelection::IsSelected()
  315. {
  316.     return (!Nlm_EmptyRect( &fSelrect));
  317.     //return (fSelectedRow == kNoSelection && fSelectedCol == kNoSelection);
  318.  
  319.  
  320. Boolean DTabSelection::IsSelected(long row, long col)
  321. {
  322.     Nlm_PoinT pt;
  323.     Boolean issel;
  324.     pt.y= row; pt.x= col;
  325.     if (!Nlm_EmptyRgn(fSelrgn)) issel= Nlm_PtInRgn( pt, fSelrgn);
  326.     else issel= Nlm_PtInRect( pt, &fSelrect);
  327.     return issel;
  328. }  
  329.  
  330. Nlm_RecT DTabSelection::GetSelRect() const
  331.     return fSelrect; 
  332. }
  333.  
  334. long DTabSelection::GetSelectedRow()
  335.     return fSelrect.top; //fSelectedRow; 
  336. }
  337.  
  338. long DTabSelection::GetSelectedCol()  
  339.     return fSelrect.left; //fSelectedCol; 
  340. }
  341.  
  342. void DTabSelection::GetFirstSelectedCell( long& row, long& col)
  343. #if 0
  344.     row= fSelectedRow; 
  345.     col= fSelectedCol; 
  346. #else
  347.     row= fSelrect.top;
  348.     col= fSelrect.left;
  349. #endif
  350. }
  351.  
  352. void DTabSelection::GetLastSelectedCell( long& row, long& col)
  353. #if 0
  354.     row= fSelectedRow; 
  355.     col= fSelectedCol; 
  356. #else
  357.     row= fSelrect.bottom;
  358.     col= fSelrect.right;
  359. #endif
  360. }
  361.     
  362. void DTabSelection::InvertSelection()
  363. {    
  364.     InvertSelection(NULL);
  365. }
  366.  
  367. void DTabSelection::InvertSelection(Nlm_RegioN updateRgn)
  368. {    
  369.     Nlm_RecT r, r2;
  370.  
  371.     if (!Nlm_EmptyRgn( fSelrgn)) {
  372. #if 0    
  373.                 // this takes much much tooooooo lonnnnggggg to draw
  374.         Nlm_ClearRgn( fWorkrgn);
  375.         Nlm_UnionRgn( fWorkrgn, fSelrgn, fWorkrgn);
  376.         Nlm_RegioN pixrgn= Nlm_CreateRgn();
  377.         fTable->GetPixRgn( fSelrect, fWorkrgn, pixrgn);
  378.         if (updateRgn) Nlm_SectRgn( pixrgn, updateRgn, pixrgn);
  379.         fTable->InvertRgn( pixrgn);
  380.       pixrgn= Nlm_DestroyRgn( pixrgn);
  381. #endif
  382.       return;
  383.         }
  384.     else if (!Nlm_EmptyRect(&fSelrect)) {
  385.         r2= fSelrect;
  386.         //r2.bottom--;
  387.         //r2.right--;
  388.         fTable->GetCellRect( r2, r);
  389.         }
  390.     else if (fSelectedRow>=0 && fSelectedCol>=0) 
  391.         fTable->GetCellRect( fSelectedRow, fSelectedCol, r);
  392.     else if (fSelectedRow>=0) 
  393.         fTable->GetRowRect( fSelectedRow, r);
  394.     else if (fSelectedCol>=0) 
  395.         fTable->GetRowRect( fSelectedCol, r);
  396.   else
  397.       return;
  398.     fTable->InvertRect( r);
  399. }
  400.  
  401.  
  402.  
  403.